package com.wali.common.ccp.json;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.wali.common.ccp.exception.CcpException;
import com.wali.common.enums.ErrorCodeEnum;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * JSON转换工具
 *
 * @author wali
 * @version [版本号, 2018年07月11日]
 */
public class CcpFastjsonkMapper {

    /**
     * 将json字符串转换为map
     *
     * @throws CcpException
     * @author wali
     * @version [版本号, 2018年07月11日]
     */
    public static Map<String, String> convertStringToMap(String str)
            throws CcpException {
        if (StringUtils.isEmpty(str)) {
            throw new CcpException(ErrorCodeEnum.ERROR_JSON_PARAMS_ILLEGAL);
        }

        try {
            return JSON.parseObject(str, new TypeReference<Map<String, String>>() {
            });
        } catch (Exception e) {
            throw new CcpException(ErrorCodeEnum.ERROR_JSON_CONVERT_FAIL, e);
        }
    }

    /**
     * 将json字符串转换为map
     *
     * @throws CcpException
     * @author wali
     * @version [版本号, 2018年07月11日]
     */
    public static Map<String, Object> convertStringToMapObj(String str)
            throws CcpException {
        if (StringUtils.isEmpty(str)) {
            throw new CcpException(ErrorCodeEnum.ERROR_JSON_PARAMS_ILLEGAL);
        }

        try {
            return JSON.parseObject(str, new TypeReference<Map<String, Object>>() {
            });
        } catch (Exception e) {
            throw new CcpException(ErrorCodeEnum.ERROR_JSON_CONVERT_FAIL, e);
        }
    }

    /**
     * 将json字符串转换成指定类型的对象
     *
     * @throws CcpException
     * @author wali
     * @version [版本号, 2018年07月11日]
     */
    public static <T> T convertStringToObject(String str, Class<T> requiredType)
            throws CcpException {
        if (StringUtils.isEmpty(str)) {
            throw new CcpException(ErrorCodeEnum.ERROR_JSON_PARAMS_ILLEGAL);
        }

        try {
            return JSON.parseObject(str, requiredType);
        } catch (Exception e) {
            throw new CcpException(ErrorCodeEnum.ERROR_JSON_CONVERT_FAIL, e);
        }
    }

    /**
     * 将json字符串数组转换成List
     *
     * @throws CcpException
     * @author wali
     * @version [版本号, 2018年07月11日]
     */
    public static List<Map<String, String>> convertStringToList(String str)
            throws CcpException {
        if (StringUtils.isEmpty(str)) {
            throw new CcpException(ErrorCodeEnum.ERROR_JSON_PARAMS_ILLEGAL);
        }

        try {
            return JSON.parseObject(str, new TypeReference<List<Map<String, String>>>() {
            });
        } catch (Exception e) {
            throw new CcpException(ErrorCodeEnum.ERROR_JSON_CONVERT_FAIL, e);
        }
    }

    /**
     * 将对象转换成json字符串
     *
     * @throws CcpException
     * @author 徐干华 0118
     * @version [版本号, 2018年07月11日]
     */
    public static String convertObjectToString(Object obj)
            throws CcpException {
        if (obj == null) {
            throw new CcpException(ErrorCodeEnum.ERROR_JSON_PARAMS_ILLEGAL);
        }

        try {
            return JSONObject.toJSONString(obj);
        } catch (Exception e) {
            throw new CcpException(ErrorCodeEnum.ERROR_JSON_CONVERT_FAIL, e);
        }

    }

}
